package com.tech.connect.zhaorencai;

import android.content.Context;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ksy.common.utils.BaseAdapter;
import com.ksy.common.utils.BaseHolder;
import com.ksy.common.view.HLineDivider;
import com.tech.connect.R;
import com.tech.connect.view.IndexImageLayout;

import java.util.ArrayList;
import java.util.List;

public class JianLiView extends NestedScrollView {

    private ImageView ivSex;
    private TextView tvName;
    private TextView tvAddress;
    private TextView tvSign;
    private TextView tvJob;
    private TextView tvMoney;
    private TextView tvDate;
    private TextView tvStudy;
    private TextView tvBir;
    private View llJob;
    private ImageView ivMoreJob;
    private RecyclerView jobRecycler;
    private View llStudy;
    private ImageView ivMoreStudy;
    private RecyclerView studyRecycler;
    private TextView tvNote;
    private List<String> allJobSrcList = new ArrayList<>();
    private List<String> allJobList = new ArrayList<>();
    private List<String> allStudySrcList = new ArrayList<>();
    private List<String> allStudyList = new ArrayList<>();
    private BaseAdapter<String, BaseHolder> jobAdapter;
    private BaseAdapter<String, BaseHolder> studyAdapter;
    private IndexImageLayout viewPager;

    public JianLiView(Context context) {
        this(context, null);
    }

    public JianLiView(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.layout_jianli, this, true);
        initView();
    }

    private void initView() {
        viewPager = findViewById(R.id.viewPager);
        tvName = findViewById(R.id.tvName);
        ivSex = findViewById(R.id.ivSex);
        tvAddress = findViewById(R.id.tvAddress);
        tvSign = findViewById(R.id.tvSign);
        tvJob = findViewById(R.id.tvJob);
        tvMoney = findViewById(R.id.tvMoney);
        tvDate = findViewById(R.id.tvDate);
        tvStudy = findViewById(R.id.tvStudy);
        tvBir = findViewById(R.id.tvBir);
        llJob = findViewById(R.id.llJob);
        ivMoreJob = findViewById(R.id.ivMoreJob);
        ivMoreJob.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                llJob.performClick();
            }
        });
        jobRecycler = findViewById(R.id.jobRecycler);
        jobRecycler.addItemDecoration(new HLineDivider());
        jobRecycler.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        llStudy = findViewById(R.id.llStudy);
        ivMoreStudy = findViewById(R.id.ivMoreStudy);
        ivMoreStudy.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                llStudy.performClick();
            }
        });
        studyRecycler = findViewById(R.id.studyRecycler);
        studyRecycler.addItemDecoration(new HLineDivider());
        studyRecycler.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        tvNote = findViewById(R.id.tvNote);

        allJobList.add("");
        allJobList.add("");
        allJobList.add("");

        allStudyList.add("");
        allStudyList.add("");
        if (allJobList.size() <= 1) {
            ivMoreJob.setVisibility(GONE);
        } else {
            llJob.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    boolean selected = ivMoreJob.isSelected();
                    if (selected) {
                        ivMoreJob.setSelected(false);
                        ivMoreJob.setImageResource(R.drawable.icon_arrow_right);
                        //收起
                        String strings = allJobList.get(0);
                        List<String> stringList = new ArrayList<>();
                        stringList.add(strings);
                        initJobAdapter(stringList);
                    } else {
                        ivMoreJob.setSelected(true);
                        //展开
                        ivMoreJob.setImageResource(R.drawable.icon_arrow_down);
                        initJobAdapter(allJobList);
                    }
                }
            });
        }
        if (allStudyList.size() <= 1) {
            ivMoreStudy.setVisibility(GONE);
        } else {
            llStudy.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    boolean selected = ivMoreStudy.isSelected();
                    if (selected) {
                        ivMoreStudy.setSelected(false);
                        //收起
                        ivMoreStudy.setImageResource(R.drawable.icon_arrow_right);
                        String strings = allStudyList.get(0);
                        List<String> stringList = new ArrayList<>();
                        stringList.add(strings);
                        initStudyAdapter(stringList);
                    } else {
                        ivMoreStudy.setSelected(true);
                        //展开
                        ivMoreStudy.setImageResource(R.drawable.icon_arrow_down);
                        initStudyAdapter(allStudyList);
                    }
                }
            });
        }
        if (!allJobList.isEmpty()) {
            String strings = allJobList.get(0);
            List<String> stringList = new ArrayList<>();
            stringList.add(strings);
            initJobAdapter(stringList);
        }
        if (!allStudyList.isEmpty()) {
            String strings = allStudyList.get(0);
            List<String> stringList = new ArrayList<>();
            stringList.add(strings);
            initStudyAdapter(stringList);
        }

        List<String> strings = new ArrayList<>();
        strings.add("");
        strings.add("");
        strings.add("");
        strings.add("");
        strings.add("");
        viewPager.updateImages(strings,0);
    }

    private void initJobAdapter(List<String> list) {
        allJobSrcList.clear();
        if (list != null) {
            allJobSrcList.addAll(list);
        }
        if (jobAdapter == null) {
            jobAdapter = new BaseAdapter<String, BaseHolder>(R.layout.item_layout_jinli, allJobSrcList) {
                @Override
                protected void convert(BaseHolder helper, int position) {
//                    String s = allJobList.get(position);
//                    TextView tv = helper.getView(R.id.tv);
//                    tv.setText(s);
                }
            };
            jobRecycler.setAdapter(jobAdapter);
        }
        jobAdapter.notifyDataSetChanged();
    }

    private void initStudyAdapter(List<String> list) {
        allStudySrcList.clear();
        if (list != null) {
            allStudySrcList.addAll(list);
        }
        if (studyAdapter == null) {
            studyAdapter = new BaseAdapter<String, BaseHolder>(R.layout.item_layout_jinli, allStudySrcList) {
                @Override
                protected void convert(BaseHolder helper, int position) {
//                    String s = allJobList.get(position);
//                    TextView tv = helper.getView(R.id.tv);
//                    tv.setText(s);
                }
            };
            studyRecycler.setAdapter(studyAdapter);
        }
        studyAdapter.notifyDataSetChanged();
    }

    public void update() {

    }


}
